import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lys
 * 借阅记录页面
 */
public class BorrowRecords extends JFrame {
    private static BorrowRecords instance;
    private JPanel jPanel = new JPanel();
    private Font font = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,30);

    private JLabel jLabel = new JLabel("我的借阅");

    public DefaultTableModel model = new DefaultTableModel();

    private JButton jButton = new JButton("返回");

    private BorrowRecords() {
        Icon i = new ImageIcon(BorrowRecords.class.getResource("img/5.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        jLabel.setFont(font);
        jLabel.setBounds(450, 50, 800, 100);
        jLabel.setForeground(Color.white);

        model.addColumn("书号");
        model.addColumn("书名");
        model.addColumn("借书人");
        model.addColumn("借书日期");
        model.addColumn("是否归还");
        model.addColumn("还书日期");

        JTable jTable = new JTable(model);
        jTable.setOpaque(false);

        JScrollPane pane = new JScrollPane(jTable);
        pane.setBounds(200,250,800,400);
        pane.setOpaque(false);

        JTableHeader header = jTable.getTableHeader();
        header.setPreferredSize(new Dimension(header.getWidth(),30));
        header.setBackground(Color.white);
        jTable.setRowHeight(30);
        jTable.setFont(new Font("宋体",Font.ROMAN_BASELINE,17));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        jTable.setDefaultRenderer(Object.class, renderer);


        jButton.setFont(font2);
        jButton.setBounds(950,670,100,50);
        jButton.setForeground(Color.black);
        jButton.setFocusPainted(false);
        //jButton.setOpaque(false);

        add(jLabel);
        add(pane);
        add(jButton);
        add(Label);

        setSize(1200,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ArrayList<BorrowInfo> borrowRecords = returnBorrowRecords();
        DefaultTableModel model2 = (DefaultTableModel) jTable.getModel();
        model2.getDataVector().clear();
        model2.fireTableDataChanged();
        jTable.updateUI();
        for(BorrowInfo borrowInfo: borrowRecords){
            String[] column = {String.valueOf(borrowInfo.getBookId()), borrowInfo.getBookName(), borrowInfo.getBorrowUser(), String.valueOf(borrowInfo.getBorrowTime()), "", ""};
            if(borrowInfo.getReturned()){
                column[4] = "已还";
            }
            else{
                column[4] = "未还";
            }
            if(borrowInfo.getReturnTime() != null){
                column[5] = String.valueOf(borrowInfo.getReturnTime());
            }
            model2.addRow(column);
        }

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                UserInterface.getInstance().user.setVisible(true);
            }
        });

        jButton.setFocusPainted(false);
    }

    public static BorrowRecords  getInstance() {
        if (instance == null) instance = new BorrowRecords();
        return instance;
    }

//    public static void main(String[] args) {
//        BorrowRecords.getInstance();
//    }
    /**
     * 展示用户个人的借书记录
     * @author zj
     */
    public ArrayList<BorrowInfo> returnBorrowRecords(){
        ResultSet rs = null;
        PreparedStatement preSql;
        String sql;
        String username = Login.loginUser;
        Connection con = DatabaseConnection.getInstance().getCon();
        sql = "select * from borrowinfo where borrowuser = ?";
        ArrayList<BorrowInfo> borrowRecords = new ArrayList<>();
        try{
            preSql = con.prepareStatement(sql);
            preSql.setString(1, username);
            rs = preSql.executeQuery();
            while(rs.next()){
                int bookId = rs.getInt("bookid");
                String bookName = rs.getString("bookname");
                String borrowUser = rs.getString("borrowuser");
                Date borrowTime = rs.getDate("borrowtime");
                Boolean isReturned = rs.getBoolean("isreturned");
                Date returnTime = rs.getDate("returntime");
                BorrowInfo borrowInfo = new BorrowInfo(bookId, bookName, borrowUser, borrowTime, isReturned, returnTime);
                borrowRecords.add(borrowInfo);
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return borrowRecords;
    }

    public static void removeInstance(){
        instance = null;
    }
}
